using System;

using System.Text;
using System.Text.RegularExpressions;

namespace PointBridge.SPDX
{
    public class Common
    {
        static public bool ValidateRx(string input, string pattern)
        {
            Match m = Regex.Match(input, pattern);
            if (m.Success == false) return false;
            if (m.Length != input.Length) return false;
            return true;
        }

        static public string PrintResponseCode(int code)
        {
            switch (code)
            {
                case -1: return "";
                case 1: return "Approved";
                case 2: return "Declined";
                case 3: return "Error";
                case 4: return "On Hold";
                default: return "Undefined";
            }
        }

        static public string PrintMMYYYY(DateTime val)
        {
            string ret;
            if (IsMaxDate(val) == true) ret = "";
            else ret = val.Month + "/" + val.Year;
            return ret;
        }
        static public string PrintMMDDHHMM(DateTime val)
        {
            string ret;
            if (IsMaxDate(val) == true) throw new Exception("PrintMMDDHHMM does not allow MaxDate");
            else ret = val.ToString("M/d h:mm tt CST");
            return ret;
        }
        static public string Print(DateTime val)
        {
            string ret;
            if (Common.IsMaxDate(val) == true) ret = "";
            else ret = val.ToShortDateString();
            return ret;
        }

        static public string Print(int val)
        {
            string ret;
            if (val < 0) ret = "";
            else ret = val.ToString();
            return ret;
        }

        static public string PrintF0(float val)
        {
            string ret;
            if (val < 0) ret = "";
            else ret = Math.Round(val, 0).ToString("F0");
            return ret;
        }
        static public string PrintF0Negative(float val)
        {
            return Math.Round(val, 0).ToString("F0");
        }

        static public string PrintF1(float val)
        {
            string ret;
            if (val < 0) ret = "";
            else ret = Math.Round(val, 1).ToString("F1");
            return ret;
        }

        static public string PrintF2Negative(float val)
        {
            return Math.Round(val, 2).ToString("F2");
        }

        static public string PrintF2(float val)
        {
            string ret;
            if (val < 0) ret = "";
            else ret = Math.Round(val, 2).ToString("F2");
            return ret;
        }


        static public string PrintD2(int val)
        {
            return val.ToString("D2");
        }
        static public string PrintD4(int val)
        {
            return val.ToString("D4");
        }
        static public string PrintD5(int val)
        {
            return val.ToString("D5");
        }
        static public string PrintD6(int val)
        {
            return val.ToString("D6");
        }
        static public string PrintDollarAmountF2(float val)
        {
            string strVal = PrintF2(val);

            string Afterdot = strVal.Substring(strVal.IndexOf("."), 3);
            strVal = strVal.Substring(0, strVal.IndexOf("."));
            int CommaPos;

            if (strVal.Length > 3)
            {
                int MaxCommas = (strVal.Length / 3) - 1;

                if ((strVal.Length % 3) == 0) MaxCommas = MaxCommas - 1;

                for (int i = 0; i <= MaxCommas; i++)
                {
                    CommaPos = ((i + 1) * 3) + i;
                    strVal = strVal.Substring(0, strVal.Length - CommaPos) + "," + strVal.Substring(strVal.Length - CommaPos, CommaPos);
                }
            }
            return (strVal + Afterdot);
        }

        static public string PrintF3Negative(float val)
        {
            return Math.Round(val, 3).ToString("F3");
        }

        static public string PrintF3NegativePlus(float val)
        {
            string ret = Math.Round(val, 3).ToString("F3");
            if (val > 0) ret = "+" + ret;
            return ret;
        }

        static public string PrintF3(float val)
        {
            string ret;
            if (val < 0) ret = "";
            else ret = Math.Round(val, 3).ToString("F3");
            return ret;
        }

        public static bool IsMaxDate(DateTime val)
        {
            return (val.ToString() == DateTime.MaxValue.ToString());
        }

        public static string PrepareForDebugOutput(string inStr)
        {
            inStr = inStr.Replace("\\\"", "'").Replace("\"", "'");
            inStr = ReplaceWhiteSpaceWithSingleSpace(inStr);
            return inStr;
        }
        public static string ReplaceWhiteSpaceWithSingleSpace(string inStr)
        {
            StringBuilder outStr = new StringBuilder();
            bool inWhiteSpace = false;
            for (int i = 0; i < inStr.Length; i++)
            {
                if (char.IsWhiteSpace(inStr[i]) == true)
                {
                    if (inWhiteSpace != true)
                    {
                        outStr.Append(" ");
                        inWhiteSpace = true;
                    }
                }
                else
                {
                    outStr.Append(inStr[i]);
                    inWhiteSpace = false;
                }
            }
            return outStr.ToString();
        }

        public static bool IsGuid(string expression)
        {
            if (expression != null)
            {
                Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

                return guidRegEx.IsMatch(expression);
            }
            return false;
        }

        public static float RandomPercent()
        {
            return random.Next(0, 100);
        }
        public static string RandomDigit()
        {
            char ch = (char)random.Next('0', '9');
            return ch.ToString();
        }
        public static string RandomLetter()
        {
            char ch = (char)random.Next('a', 'z');
            return ch.ToString();
        }
        static readonly Random random = new Random();
    }
}
